#include "StdAfx.h"
#include "PopupAutoModeThread.h"
#include "popup.h"
#include "popupdlg.h"
#include <iostream>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

using namespace std;

IMPLEMENT_DYNCREATE(PopupAutoModeThread, CWinThread)

BOOL PopupAutoModeThread::InitInstance()
{
	return TRUE;
}

int PopupAutoModeThread::Run()
{
	CpopupApp *_app = (CpopupApp*)AfxGetApp();
	CpopupDlg *_dlg = _app->popupdlg;

	// Constants
	PopupClientMode & _automode = _app->m_automode;
	int & _delay = _app->m_automodeDelay;

	// Variables
	bool & _reset = _dlg->m_resetTimer;
	PopupClientMode & _currentmode = _dlg->m_mode;

	while (true)
	{
		// Nothing to do while we are already in the
		// good mode
		while (_currentmode == _automode) {
			Sleep(5000);
		}

		// Let's start countdown...
		size_t _countdown = _delay;
		while (_countdown > 0)
		{
			if (_reset) {
				_reset = false;
				_countdown = _delay;
			} else {
				Sleep(5000);
				_countdown -= 5;
			}
		}

		// End of loop we switch to auto mode
		_dlg->setMode(_automode);
	}

	return 0;
}
